<?php
/**
 * Created by xun at 2020/5/21
 */
namespace backend\models\c2\entity;
use common\models\c2\entity\InventoryDeliveryNoteItem;
use common\models\c2\entity\WarehouseNote;
use common\models\c2\entity\WarehouseNoteItem;
use common\models\c2\statics\WarehouseNoteState;
use yii\db\Exception;

class DeliveryNoteOutput extends WarehouseNote
{

    public function getDeliveryNoteItems()
    {
        return $this->hasMany(InventoryDeliveryNoteItem::className(), ['note_id' => 'ref_note_id']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if ($insert) {
            $items = $this->getDeliveryNoteItems()->asArray()->all();
            if (!empty($items)) {
                foreach ($items as $item) {
                    $attributes = [
                        'warehouse_note_id' => $this->id,
                        'product_id' => $item['product_id'],
                        'product_sku_id' => $item['product_sku_id'],
                        'measure_id' => $item['measure_id'],
                        'pieces' => $item['pieces'],
                        'quantity' => $item['quantity'],
                    ];
                    $itemModel = new WarehouseNoteItem();
                    $itemModel->setAttributes($attributes);
                    $itemModel->link('owner', $this);
                }
            }
        } else {
            $items = $this->items;
            if (!empty($items)) {
                foreach ($items as $item) {
                    $attributes = [
                        'warehouse_note_id' => $this->id,
                        'product_id' => $item['product_id'],
                        'product_sku_id' => $item['product_sku_id'],
                        'measure_id' => $item['measure_id'],
                        'pieces' => $item['pieces'],
                        'quantity' => $item['quantity'],
                    ];
                    if (isset($item['id']) && $item['id'] == '') {
                        $itemModel = new WarehouseNoteItem();
                        $itemModel->setAttributes($attributes);
                        $itemModel->link('owner', $this);
                    } else {
                        $itemModel = WarehouseNoteItem::findOne($item['id']);
                        if (!is_null($itemModel)) {
                            $itemModel->updateAttributes($attributes);
                        }
                    }
                }
            }
        }
    }

    public function commit()
    {
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $items = $this->getWarehouseNoteItems()->all();
            foreach ($items as $item) {
                $item->productSku->updateCounters([
                    'stock' => -($item->quantity)
                ]);
            }
            $this->updateAttributes([
                'state' => WarehouseNoteState::COMMIT
            ]);
            $transaction->commit();
            return true;
        } catch (Exception $exception) {
            $transaction->rollBack();
        }
        return null;
    }

    public function beforeDelete()
    {
        foreach ($this->getDeliveryNoteItems()->all() as $item) {
            $item->delete();
        }
        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }

}